//
//  HMWelcomeViewController.swift
//  WeiBo
//
//  Created by Apple on 16/9/20.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  欢迎页面
class HMWelcomeViewController: UIViewController {

    // MARK:    --懒加载
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var userImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = imageView.size.width / 2
        imageView.layer.masksToBounds = true
        return imageView
    
    }()
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        //  默认透明度是0
        label.alpha = 0
        return label
    }()
    
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    private func setupUI() {
    
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        //  设置约束
        userImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView)
            make.top.equalTo(userImageView.snp_bottom).offset(10)
        }
        
        
        
    }
    
    //  页面已经显示
    override func viewDidAppear(_ animated: Bool) {
       
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation() {
        
        //  1.更新约束
        userImageView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        
        //  usingSpringWithDamping 阻尼 0-1
        //  initialSpringVelocity 初始速度
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
               UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1
                }, completion: { (_) in
                    print("弹簧动画执行完毕")
                    
                    //  切换跟视图控制器
                    NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotification), object: nil)
               })
                
                
        }
        
        
    }
    
    
    
    
    
    
    
    
    

   

}
